/**
 *  @name 请求
 */
async function apiPost(url, params, methods) {
	if (!params) console.warn("没有传入参数params");
	const appInstance = getApp();
	if (appInstance.globalData.token == '' || appInstance.globalData.url == '') {
		uni.redirectTo({
			url: '../index/index'
		})
		return
	}
	return await new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: methods,
			header: {
				"content-type": "application/json; charset=UTF-8",
				"X-Access-Token": appInstance.globalData.token
			},
			data: params,
			success(res) {
				//认证失败时，跳转到登录页面
				if (res.statusCode === 401) {
					console.log('登录过期')
					const appInstance = getApp();
					appInstance.globalData.token = ''
					appInstance.globalData.url = ''
					uni.redirectTo({
						url: '../index/index'
					});
				}
				resolve(res.data)
			},
			fail(err) {
			   reject(err);
			}
		})
	})
}

async function showLoading() {
	uni.showLoading({
		title: "请等待",
		mask: true,
	})
}

/**
 * @name 显示toast
 * @param res {status:"0",(msg||data):""}
 */
async function showToast(res) {
	uni.showToast({
		title: (res.msg || res.data).toString().slice(0, 7),
		mask: true,
		icon: res.status == 0 ? "success" : "",
		duration: 1500
	});
}

export default {
	apiPost,
}